<script setup lang="ts">
import type { SupportedLanguagesType } from '#/locales';

import { SUPPORT_LANGUAGES } from '#/constants';
import { Languages } from '#/icons';
import { loadLocaleMessages } from '#/locales';
import { preferences, updatePreferences } from '#/preferences';

import { BasicDropdownRadioMenu, BasicIconButton } from '#/components/shadcn-ui';

defineOptions({
  name: 'LanguageToggle',
});

async function handleUpdate(value: string) {
  const locale = value as SupportedLanguagesType;
  updatePreferences({
    app: {
      locale,
    },
  });
  await loadLocaleMessages(locale);
}
</script>

<template>
  <div>
    <BasicDropdownRadioMenu
      :menus="SUPPORT_LANGUAGES"
      :model-value="preferences.app.locale"
      @update:model-value="handleUpdate"
    >
      <BasicIconButton>
        <Languages class="text-foreground size-4" />
      </BasicIconButton>
    </BasicDropdownRadioMenu>
  </div>
</template>
